package com.stlm2.controller.commission;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.Commission;
import com.stlm2.dems.service.CommissionService;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author：fuchunyu
 * Date：2017/9/13
 * Description：
 */
@Controller
@RequestMapping("/commissionC")
public class CommissionController {

    @Resource
    private UserService userService;
    @Resource
    private CommissionService commissionService;

    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/commission/list");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommission.json",produces="application/json;charset=UTF-8")
    public String queryCommission(String phone,String page){

        PageView pageview = new PageView();
        int pageNo = 1;
        int pageSize = 20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        Commission commission=new Commission();
        if(StringUtils.isNotEmpty(phone)){
            User user=userService.getByName(phone);
            commission.setUpperUserId(user.getId());
        }

        PageView pageView=commissionService.getPage(pageNo,pageSize,commission);
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("data",commissionService.conventAll(pageView.getRecords()));
        result.put("recordsTotal", pageView.getRowCount());
        result.put("totalPage", pageView.getPageCount());
        return CodeContent.returnSuccess(result);


    }
}
